/*
 * qc slave mode data control.
 * Copyright (c) 2012  everyone
 * All rights reserved.
 * no limite
 */
#include "qcom.h"
#include "mb_box_input.h"
#include "par_tab.h"
#include "par_prog.h"
#include "sys_task.h"
#include "mb_box_hold.h"

/*******************************************************************************
 * @brief  arg
 *****************************************************************************/
static uu08 text_change = 0;

/*******************************************************************************
 * @brief  function
 *****************************************************************************/

/*******************************************************************************
 * @brief  reg_hold_read
 * \param[in] Index : data address
 * @return  data
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
uint16_t qc_reg_par_get(uint16_t Index)
{
	word_tt Val = {0};

	// 系统参数: 0x0000-0x0FFF
	Index &= 0x7FF;

	// 系统参数
	if (Index < 0x200)
	{
		uu16 par_num = par_addr_dict[Index];

		if (par_num)
		{
			Val.uval = par_tab[par_num].uval;
		}
	}
	else if (Index < 0x400)
	{
	}

#if TEXT_PAR_EN > 0
	// 文本参数
	else if (Index < 0x400 + 0x10 * 32)
	{
		uu16 dict_pos, offset;
		uu16 par_num;

		dict_pos = (Index - 0x400) >> 4;
		offset = (Index - 0x400) & 0x0F;
		par_num = par_text_dict[dict_pos];

		if (par_num >= 0 && offset < sizeof(sys_txt_type) / 2)
		{
			Val.uval = ((uu16 *)text_tab[par_num])[offset];
		}
	}
#endif

	return Val.uVal;
}

/*******************************************************************************
 * @brief  reg_hold_write
 * \param[in] Index : data address
 * \param[in] Val : data
 * @return  data
 * @ Pass/ Fail criteria: none
 *******************************************************************************/
void qc_reg_par_set(uint16_t Index, uint16_t Val)
{
	u16_tt vset;

	vset.uval = Val;

	// 系统参数: 0x0000-0x0FFF

	// 2K 个通道划分为一个区。 0区掉电记忆，1区不记忆
	uu16 zone = Index >> 11, sub = Index & 0x7FF;
	uu16 par_num, attr;

	if (sub < 0x200)
	{
		par_num = par_addr_dict[Index];
		attr = par_attr_get(par_num);

		if (par_num)
		{
			if (0 == (attr & PAR_ATTR_NEDIT))
			{
				if (attr & PAR_ATTR_UNSIGNED)
				{
					if (0 == zone)
						par_val_set(par_num, par_correct(par_num, vset.uval));
					else
						par_val_set_ext(par_num, par_correct(par_num, vset.uval), DIS);
				}
				else
				{
					if (0 == zone)
						par_val_set(par_num, par_correct(par_num, vset.ival));
					else
						par_val_set_ext(par_num, par_correct(par_num, vset.ival), DIS);
				}
			}
		}
	}
	else if (sub < 0x400)
	{
	}

#if TEXT_PAR_EN > 0
	else if (sub < 0x400 + 0x10 * 32)
	{
		uu16 dict_pos, offset;
		uu16 par_num;

		dict_pos = (Index - 0x400) >> 4;
		offset = (Index - 0x400) & 0x0F;
		par_num = par_text_dict[dict_pos];
		attr = par_text_attr_get(par_num);

		if (par_num >= 0 && offset < sizeof(sys_txt_type) / 2 && (0 == (attr & PAR_ATTR_NEDIT)))
		{
			if (offset == 0)
				text_change = 0;

			if (((uu16 *)text_tab[par_num])[offset] != vset.uval)
			{
				((uu16 *)text_tab[par_num])[offset] = vset.uval;
				text_change = 1;
			}

			if (0 == zone && text_change && offset == sizeof(sys_txt_type) / 2 - 1)
			{
				par_text_save(par_num);
			}
		}
	}
#endif
}
